from ObjectIndexer import *

class MetaWidgetTree:
    def __init__(self):
        self.root_meta_widget = None
        
        self.meta_widget_index = ObjectIndexer()

    def getRootMetaWidget(self):
        return self.root_meta_widget
    
    def setLink(self, parent_meta_widget, child_meta_widget):
        if parent_meta_widget != None: # when meta-widget is not a root
            child_meta_widget.setParentMetaWidget(parent_meta_widget)
            parent_meta_widget.appendChildMetaWidget(child_meta_widget)
        else: # when meta-widget is a root
            self.root_meta_widget = child_meta_widget

    def getMetaWidgetByName(self, name):
        return self.meta_widget_index.find(name)

    def getWidgetByName(self, name):
        meta_widget = self.meta_widget_index.find(name)
        
        if meta_widget != None:
            return meta_widget.getWidget()
        else:
            return None
            
    # change function name to "indexMetaWidgetByName"
    def setNameIndexForMetaWidget(self, meta_widget, name):
        self.meta_widget_index.set(name, meta_widget)
        
    def getIndexer(self):
        return self.meta_widget_index